﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;

namespace DockSample.DAO
{
    class NonFunctionRequirmentDAO:BaseDAO
    {
        OriginalNFRChartTypeDAO chartDAO = new OriginalNFRChartTypeDAO();

        public bool DeleteNonFunctionRequirment(NonFunctionRequirment nfr)
        {
            try
            {

                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "delete from NonFunctionRequirment where id=@id";
                cmd.Parameters.Add(new SQLiteParameter("id", nfr.id));

                cmd.ExecuteNonQuery();


                return true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
            finally
            {
                this.CloseConnection();
            }
        }

        public bool AddNonFunctionRequirment(NonFunctionRequirment nfr, String objId)
        {
            try
            {
         //       this.DeleteNonFunctionRequirment(nfr);
                //   this.conn = this.GetConnection();
                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "insert into NonFunctionRequirment( id ,objId,nonFunctionType,nonFunctionName,nonFunctionValue,description)  "
                    + "values ( @id ,@objId,@nonFunctionType,@nonFunctionName,@nonFunctionValue,@description)";
                cmd.Parameters.Add(new SQLiteParameter("id", nfr.id));
                cmd.Parameters.Add(new SQLiteParameter("objId", objId));
                cmd.Parameters.Add(new SQLiteParameter("nonFunctionType", nfr.NonFunctionType));
                cmd.Parameters.Add(new SQLiteParameter("nonFunctionName", nfr.nonFunctionName));
                cmd.Parameters.Add(new SQLiteParameter("nonFunctionValue", nfr.nonFunctionValue));
                cmd.Parameters.Add(new SQLiteParameter("description", nfr.description));
               
                cmd.ExecuteNonQuery();

                this.SaveRelations(nfr);

                return true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
            finally
            {
                this.CloseConnection();
            }

        }

        public List<NonFunctionRequirment> GetNFRByObjId(string objId)
        {
            try
            {
                List<NonFunctionRequirment> list = new List<NonFunctionRequirment>();


                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "select * from NonFunctionRequirment where objId=@objId";
                cmd.Parameters.Add(new SQLiteParameter("objId", objId));
                SQLiteDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    NonFunctionRequirment nfr = new NonFunctionRequirment(reader["nonFunctionType"].ToString(),
                        reader["nonFunctionName"].ToString(),
                        reader["nonFunctionValue"].ToString(),
                        reader["id"].ToString(),
                        reader["description"].ToString());


                    nfr.Unit = reader["unit"].ToString();
                    string chartId = reader["chartTypeId"].ToString();
                    
                    if (chartId!=null)
                    {
                        nfr.ChartType=chartDAO.GetOriginalNFRChartTypeById(chartId);
                        
                    }

                    list.Add(nfr);

                    
                }

                return list;

            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return null;
            }
            finally
            {
                this.CloseConnection();
            }
        }

        private void SaveRelations(NonFunctionRequirment nfr)
        {
            this.SaveChartType(nfr);
            this.SaveUnit(nfr);
        }

        private void SaveChartType(NonFunctionRequirment nfr)
        {
            if (nfr.ChartType!=null)
            {
                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "update NonFunctionRequirment set chartTypeId=@chartTypeId where id=@id";
                cmd.Parameters.Add(new SQLiteParameter("chartTypeId", nfr.ChartType.id));
                cmd.Parameters.Add(new SQLiteParameter("id", nfr.id));

               
                chartDAO.AddOriginalNFRChartType(nfr.ChartType);
                cmd.ExecuteNonQuery();
            }
           
        }

        private void SaveUnit(NonFunctionRequirment nfr)
        {
            if (nfr.Unit != null)
            {
                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "update NonFunctionRequirment set unit=@unit where id=@id";
                cmd.Parameters.Add(new SQLiteParameter("unit", nfr.Unit));
                cmd.Parameters.Add(new SQLiteParameter("id", nfr.id));
                cmd.ExecuteNonQuery();
            }

        }
    }
}
